package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //1.判断当前加入到购物车中的商品是否已经存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //2.如果已经存在，只需要将数量+1
        if (list != null && list.size() > 0) {
            //查询到的是个list集合，其实每次查询，只会查询到一条数据（对应索引为0）
            ShoppingCart cart = list.get(0);
            Integer number = cart.getNumber();
            cart.setNumber(number + 1);

            //将新增的数量更新到数据库中
            shoppingCartMapper.updateNumberById(cart);
        } else {

            //3.如果不存在，需要插入一条购物车数据

            //判断本次添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //本次添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                /*shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());*/
            } else {
                //本次添加到购物车的套餐
                Long setmealId = shoppingCartDTO.getSetmealId();//从前段请求中得到套菜id

                Setmeal setmeal = setmealMapper.getById(setmealId);//通过套餐id查询该套餐
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                /*shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());*/
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 产看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(currentId)
                .build();
        List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);

        return list;
    }

    /**
     * 清理购物车
     */
    @Override
    public void cleanShoppingCart() {
        Long currentId = BaseContext.getCurrentId();

        shoppingCartMapper.delectById(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中的一个商品
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart= new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
          //判断菜品或套餐的数量
        if(list!=null && list.size()>0){
            shoppingCart = list.get(0);
            Integer number = shoppingCart.getNumber();
            if (number==1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                Long id = shoppingCart.getId();
                shoppingCartMapper.delect(id);
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可（减1）
                Integer number1 = shoppingCart.getNumber();
                shoppingCart.setNumber(number1-1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }



        }




    }
}
